package com.honglv.controller.common;

import com.honglv.context.BaseContext;
import com.honglv.exception.BaseException;
import com.honglv.pojo.dto.GetStrategyDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.dto.UpdateStrategyDTO;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.GetAllStrategiesVO;
import com.honglv.pojo.vo.GetStrategiesByCityVO;

import com.honglv.pojo.vo.GetStrategyVO;
import com.honglv.pojo.vo.PageVO;
import com.honglv.service.StrategyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Slf4j
@Api("攻略相关接口")
@RequestMapping("/strategy")
public class StrategyController {
    @Autowired
    private StrategyService strategyService;

    /**
     * 根据城市分页获取攻略
     * @param pageByCityDTO
     * @return
     */
    @GetMapping("/getStrategiesByCity")
    @ApiOperation("根据城市分页获取攻略")
    public Result<PageVO<GetStrategiesByCityVO>> getStrategiesByCity(PageByCityDTO pageByCityDTO){
        if (pageByCityDTO == null || pageByCityDTO.getPageSize() == null || pageByCityDTO.getPage() == null){
            throw new BaseException("参数为空");
        }
        PageVO<GetStrategiesByCityVO> pageVO = strategyService.getStrategiesByCity(pageByCityDTO);
        if (pageVO == null){
            return Result.success("暂无攻略数据");
        }
        return Result.success(pageVO);
    }

    @GetMapping("/getStrategy")
    @ApiOperation("获取攻略详情页")
    public Result<GetStrategyVO> getStrategy(GetStrategyDTO getStrategyDTO){
        if(getStrategyDTO == null || getStrategyDTO.getId() == null){
            throw new BaseException("参数为空");
        }
        GetStrategyVO getStrategyVO = strategyService.getStrategy(getStrategyDTO.getId());
        if (getStrategyVO == null){
            return Result.success("无该攻略数据");
        }
        return Result.success(getStrategyVO);
    }

    @GetMapping("/getTopStrategy")
    @ApiOperation("获取点赞量前四的攻略")
    public Result<java.util.List<GetAllStrategiesVO>> getTopStrategy(){
        java.util.List<GetAllStrategiesVO> list = (java.util.List<GetAllStrategiesVO>) strategyService.getTopStrategy();
        if (list == null){
            return Result.success("暂无攻略数据");
        }
        return Result.success(list);
    }

    @GetMapping("/getStrategiesByAuthorId")
    @ApiOperation("通过用户id获取攻略")
    public Result<java.util.List<GetAllStrategiesVO>> getStrategiesByAuthorId(){
        Long userId = BaseContext.getCurrentId();
        if(userId == null){
            throw new BaseException("用户未登录");
        }
        List<GetAllStrategiesVO> list = (List<GetAllStrategiesVO>) strategyService.getStrategiesByAuthorId(userId);
        if(list == null){
            return Result.success("用户未写作攻略");
        }
        return Result.success(list);
    }

    @GetMapping("/deleteStrategy/{id}")
    @ApiOperation("删除攻略")
    public Result deleteStrategy(@PathVariable Long id){
        return strategyService.deleteStrategy(id);
    }

    @GetMapping("/updateStrategy")
    @ApiOperation("修改攻略信息")
    public Result updateStrategy(UpdateStrategyDTO updateStrategyDTO){
        return strategyService.updateStrategy(updateStrategyDTO);
    }
}
